<template>
  <!-- 模具寿命邮件发送记录主表表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="70%"
    draggable
    @close="closeDialog"
  >
    <el-descriptions
      class="margin-top"
      title=""
      :column="2"
      size="large"
      v-show="props.isReadonly"
      border
    >
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">模具主键</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.assetId }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">模具编码</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.assetCode }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">模具名称</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.assetName }}
        </div>
      </el-descriptions-item>

      <el-descriptions-item>
        <template #label>
          <div class="cell-item">产品编码</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.productCode }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">产品名称</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.productName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">阶段</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.sendStage }}
        </div>
      </el-descriptions-item>

      <el-descriptions-item>
        <template #label>
          <div class="cell-item">位置</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.locationName }}
        </div>
      </el-descriptions-item>

      <el-descriptions-item>
        <template #label>
          <div class="cell-item">模具类型编码</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.assetTypeCode }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">模具类型名称</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.assetTypeName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">模具寿命</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.assetLifespan }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">年产量</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.yearlyOutput }}
        </div>
      </el-descriptions-item>

      <el-descriptions-item>
        <template #label>
          <div class="cell-item">登记时间</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.registrationTime }}
        </div>
      </el-descriptions-item>

      <el-descriptions-item>
        <template #label>
          <div class="cell-item">创建时间</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.createTime }}
        </div>
      </el-descriptions-item>

      <el-descriptions-item>
        <template #label>
          <div class="cell-item">创建人姓名</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.createdUserName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">最后更新时间</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.lastUpdateTime }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item span="2">
        <template #label>
          <div class="cell-item">意见</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.judgmentRemark }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">工单文件</div>
        </template>
        <div v-if="formData.files && formData.files.length">
          <CommonUploadFile
            :showText="false"
            :isRead="props.isReadonly"
            :files="formData.files"
            @update-files="updateFormFiles"
          />
        </div>
        <div v-else>无</div>
      </el-descriptions-item>
    </el-descriptions>
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="160"
      v-show="!props.isReadonly"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="模具编码" prop="assetCode" label-width="100px">
            <el-input
              v-model="formData.assetCode"
              placeholder="请输入模具编码"
              clearable
              :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="模具名称" prop="assetName" label-width="100px">
            <el-input
              v-model="formData.assetName"
              placeholder="请输入模具名称"
              clearable
              :disabled="true"
            />
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="产品编码" prop="productCode" label-width="100px">
            <el-input
              v-model="formData.productCode"
              placeholder="请输入产品编码"
              clearable
              :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="产品名称" prop="productName" label-width="100px">
            <el-input
              v-model="formData.productName"
              placeholder="请输入产品名称"
              clearable
              :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="提醒类型" prop="sendStage" label-width="100px">
            <el-input
              v-model="formData.sendStage"
              placeholder="请输入提醒类型"
              clearable
              :disabled="true"
            />
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="位置" prop="locationName" label-width="100px">
            <el-input
              v-model="formData.locationName"
              placeholder="请输入位置"
              clearable
              :disabled="true"
            />
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item
            label="模具类型"
            prop="assetTypeName"
            label-width="100px"
          >
            <el-input
              v-model="formData.assetTypeCode"
              placeholder=""
              clearable
              :disabled="true"
            />
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item
            label="模具寿命"
            prop="assetLifespan"
            label-width="100px"
          >
            <el-input
              v-model="formData.assetLifespan"
              placeholder=""
              clearable
              :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="年产量" prop="yearlyOutput" label-width="100px">
            <el-input
              v-model="formData.yearlyOutput"
              placeholder=""
              clearable
              :disabled="true"
            />
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item
            label="登记时间"
            prop="registrationTime"
            label-width="100px"
          >
            <el-input
              v-model="formData.registrationTime"
              placeholder=""
              clearable
              :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="意见" prop="judgmentRemark" label-width="100px">
            <el-input
              type="textarea"
              v-model="formData.judgmentRemark"
              placeholder="请填写意见"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-tabs type="border-card">
        <el-tab-pane label="工单文件">
          <CommonUploadFile
            v-if="props.visible"
            :isRead="props.isReadonly"
            :files="formData.files"
            @update-files="updateFormFiles"
          />
        </el-tab-pane>
      </el-tabs>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >保 存</el-button
        >
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="noHandleSubmitRecord"
          >判定不合格</el-button
        >
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmitRecord"
          >判定合格并关闭</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "EAMEAMMOULDEMAILRECORDADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  judgmentRemark: [{ required: true, message: "请填写意见", trigger: "blur" }],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  configId: "",
  assetId: "",
  assetCode: "",
  assetName: "",
  productId: "",
  productCode: "",
  productName: "",
  sendStage: "",
  locationId: "",
  locationName: "",
  assetTypeId: "",
  assetTypeCode: "",
  assetTypeName: "",
  assetLifespan: "",
  yearlyOutput: "",
  cumulativeUseTimes: "",
  reminderScope: "",
  registrationTime: "",
  enabled: "",
  deleted: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  files: [],
  status: "",
  judgmentRemark: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        configId,
        assetId,
        assetCode,
        assetName,
        productId,
        productCode,
        productName,
        sendStage,
        locationId,
        locationName,
        assetTypeId,
        assetTypeCode,
        assetTypeName,
        assetLifespan,
        yearlyOutput,
        cumulativeUseTimes,
        reminderScope,
        registrationTime,
        enabled,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        files,
        status,
        judgmentRemark,
      } = newVal;
      Object.assign(formData, {
        id,
        configId,
        assetId,
        assetCode,
        assetName,
        productId,
        productCode,
        productName,
        sendStage,
        locationId,
        locationName,
        assetTypeId,
        assetTypeCode,
        assetTypeName,
        assetLifespan,
        yearlyOutput,
        cumulativeUseTimes,
        reminderScope,
        registrationTime,
        enabled,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        files: files ?? [],
        status,
        judgmentRemark,
      });
      console.log("files", formData.files);
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.configId = "";
  formData.assetId = "";
  formData.assetCode = "";
  formData.assetName = "";
  formData.productId = "";
  formData.productCode = "";
  formData.productName = "";
  formData.sendStage = "";
  formData.locationId = "";
  formData.locationName = "";
  formData.assetTypeId = "";
  formData.assetTypeCode = "";
  formData.assetTypeName = "";
  formData.assetLifespan = "";
  formData.yearlyOutput = "";
  formData.cumulativeUseTimes = "";
  formData.reminderScope = "";
  formData.registrationTime = "";
  formData.enabled = "";
  formData.deleted = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.status = "";
  formData.files = [];
  formData.judgmentRemark = "";
}

const loading = ref(false);

/** 模具寿命邮件发送记录主表保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/eam/eammouldemailrecord/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

function noHandleSubmitRecord() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      formData.status = "3";
      await request({
        url: "/eam/eammouldemailrecord/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

function handleSubmitRecord() {
  if (!formData.files || formData.files.length == 0) {
    ElMessage.error("请上传至少一张图片");
    return;
  }
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      formData.status = "2";
      await request({
        url: "/eam/eammouldemailrecord/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

function updateFormFiles(val: []) {
  formData.files = val;
}
</script>
